package com.useeinfo.oa.open.admin.wuHanCommon;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.action.BaseAction;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.func.excel.ExcelExport;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.oa.common.config.ExcelExportMap;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.repair.biz.RepairOrderBiz;
import com.useeinfo.oa.modules.repair.biz.RepairOrderLogBiz;
import com.useeinfo.oa.modules.repair.entity.RepairOrder;
import com.useeinfo.oa.modules.repair.entity.RepairOrderLog;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

@Controller
@RequestMapping("/web/wuhan/automaticRepair")
public class AutomaticRepairAction extends BaseAction {

    @Autowired
    private RepairOrderBiz repairOrderBiz;
    @Autowired
    private RepairOrderLogBiz repairOrderLogBiz;

    /**
     * 打开列表页面
     */
    @RequestMapping("/getAutomaticRepairListPage")
    public ModelAndView getAutomaticRepairListPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/wuHanCommon/automaticRepair/automaticRepairList");
        return modelAndView;
    }


    /**
     * 查询列表
     */
    @RequestMapping("/listJson")
    @ResponseBody
    public JSONObject listJson(@RequestParam(value = "pageNo", required = false) String pageNowParam,
                               @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                               @RequestParam(value = "reportCode", required = false) String reportCode,
                               @RequestParam(value = "orderStatus", required = false) String orderStatus,
                               @RequestParam(value = "repairSource", required = false) String repairSource,
                               @RequestParam(value = "orderBy", required = false) String orderBy,
                               HttpServletRequest request
    ) {
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);
        if (enterprise != null) {
            queryParam.getSqlMap().put("enterpriseId", enterprise.getId() + "");
        }
        if (StringUtils.isNotBlank(orderStatus)) {
            queryParam.getSqlMap().put("repairOrderStatus", orderStatus);
        }
        queryParam.getSqlMap().put("repairSource", repairSource);
        queryParam.getSqlMap().put("reportCode", reportCode);
        if (StringUtils.isNotBlank(orderBy)) {
            queryParam.setSqlOrder("order by " + orderBy);
        }
        return repairOrderBiz.findJSONList(queryParam);
    }

    /**
     * 维修导出
     */
    @RequestMapping(value = "/exportOrder", produces = {"text/plain;charset=UTF-8"})
    @ResponseBody
    public void repairOrderExport(HttpServletResponse response, HttpServletRequest request) {
        String orderStatus = "";
        String reportCode = "";

        try {
            reportCode = URLDecoder.decode(request.getParameter("reportCode"), "UTF-8");
            orderStatus = URLDecoder.decode(request.getParameter("orderStatus"), "UTF-8");

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        QueryParam queryParam = new QueryParam();
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if (enterprise != null) {
            queryParam.getSqlMap().put("enterpriseId", enterprise.getId() + "");
        }
        queryParam.getSqlMap().put("AUTOMATIC_REPAIR", "AUTOMATIC_REPAIR");
        queryParam.getSqlMap().put("reportCode", reportCode);
        queryParam.getSqlMap().put("repairOrderStatus", orderStatus);
        JSONArray jsonArray = new JSONArray();
        List<RepairOrder> repairOrders = repairOrderBiz.findList(queryParam);

        for (RepairOrder repairOrder : repairOrders) {
            //审核时间
            QueryParam queryParam1=new QueryParam();
            queryParam1.getSqlMap().put("repairOrderLogOperationEnum","AUDIT");
            queryParam1.getSqlMap().put("repairOrderId",repairOrder.getId().toString());
            List<RepairOrderLog> repairOrderLogs=repairOrderLogBiz.findList(queryParam1);
            String time="";
            if(CollectionUtils.isNotEmpty(repairOrderLogs)){
                time= DateTimeUtils.formatDateToStringWithTime(repairOrderLogs.get(0).getCreateDate());
            }
            JSONObject jsonObject= repairOrder.toJSONObject();
            jsonObject. put("auditTime",time);
            jsonArray.add(jsonObject);
        }
        try {
            ExcelExport.exportExcelFile("维修清单", ExcelExportMap.REPAIR_ORDER_LIST_EXPORT_MAP(), jsonArray, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
